import math
import time

from random import random


def fight(character, monster, xiuxian_flag, game_speed=1):
    monster_blood_volume = monster.blood_volume
    fight_result = False
    while True:
        damage_factor = 1.0  # 伤害系数
        if character.blood_volume > 0:
            time.sleep(game_speed)
            # 致命一击： 攻击时可以暴击，暴击率为（20 + 技能lv * 5）%，暴击伤害为（30 + 技能lv * 3）%
            if character.skills[0].is_learn:
                if random() <= (0.2 + 0.05 * (character.skills[0].rank - 1)):
                    damage_factor = (1.3 + 0.03 * (character.skills[0].rank - 1))
                    character.skills[0].use()
                    # print('暴击！')

            # 蘑菇王的遗愿：遇到BOSS时伤害增加（10 + 技能lv * 5）%，可以作用于致命一击
            if monster.is_boss is True and character.skills[2].is_learn:
                damage_factor *= (1.1 + 0.05 * (character.skills[2].rank - 1))
                character.skills[2].use()
                # print('蘑菇王！')

            # 以下克上：攻击比自己等级高的敌人时有（5 + 5 * 技能lv） % 的几率增加（2 + 2 * 技能lv） % 伤害
            if monster.rank > character.rank and character.skills[4].is_learn and random() <= (
                    0.05 + 0.05 * (character.skills[4].rank - 1)):
                damage_factor += (1.02 + 0.02 * (character.skills[4].rank - 1))
                character.skills[4].use()
            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            # 玩家先攻击，结算防御后的数值
            count1 = character.attack * damage_factor - monster.defend
            if count1 < 0:
                count1 = 0

            # 嗜血   :  攻击时附带吸血，吸血量为（5 + 技能lv * 1）%攻击造成的伤害，可以作用于致命一击
            if character.skills[1].is_learn:
                blood_obtain = count1 * (0.05 + 0.01 * (character.skills[1].rank - 1))
                character.blood_volume += blood_obtain
                character.skills[1].use()
                print(' 吸血！' + '获得' + str(blood_obtain) + '生命值！')

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            # 怪物后攻击，结算防御后的数值
            count2 = monster.attack - character.defend
            if count2 < 0:
                count2 = 0

            # 复仇之心：受到攻击后(5 + 5 * 技能lv） % 的几率永久增长(2 + 2 * 技能lv）攻击力
            if character.skills[5].is_learn and random() <= (
                    0.05 + 0.05 * (character.skills[5].rank - 1)):
                character.attack += (2 + 2 * (character.skills[5].rank - 1))
                character.skills[5].use()

            # 神罗天征: 击败修罗神帝后获得该技能，攻击时有（1 + 0.01 * 技能lv） % 的几率直接秒杀怪物，包括BOSS
            if character.skills[7].is_learn and random() <= (0.01 + 0.0001 * (character.skills[7].rank - 1)):
                character.skills[7].use()
                monster_blood_volume = 0
                print("\033[0;37;41m 让世界感受痛楚！！！\033[0m")

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            monster_blood_volume -= count1
            if monster_blood_volume < 0:
                monster_blood_volume = 0

            if monster_blood_volume <= 0:
                # 修仙模式 经验累加机制
                if xiuxian_flag:
                    monster.experience += character.rank * 10
                character.experience += monster.experience
                character.gold += monster.experience
                # 获得怪物的技能
                if monster.skills is not None and len(monster.skills) > 0 and monster.skills[0]:
                    for i in character.skills:
                        if i.name == monster.skills[0].name \
                                and random() <= monster.skills_probability \
                                and i.is_learn is False:
                            i.is_learn = True
                            print('----------------------------------------------------')
                            print("\033[0;37;41m获得技能：{}\033[0m".format(monster.skills[0].name))
                            print("\033[0;37;41m{}\033[0m".format(monster.skills[0].introduction))
                            print('----------------------------------------------------')
                            print('回车键继续……')
                            input()

                # 助我成仙：击杀怪物时候有（5 + 0.5 * 技能lv）%的几率夺取怪物（10 + 10 * 技能lv）%的全属性
                if character.skills[3].is_learn:
                    if random() <= (0.05 + 0.005 * (character.skills[3].rank - 1)):
                        factor = 0.1 + 0.1 * (character.skills[3].rank - 1)
                        character.attack += monster.attack * factor
                        character.blood_volume += monster.blood_volume * factor
                        character.defend += monster.defend * factor
                        character.skills[3].use()
                        print('助我成仙！' + '获得' + monster.name, '{}%  全属性'.format(math.floor(factor * 100)))

                fight_result = True

            # 怪没死的时候计算人物受伤
            if fight_result is False:
                character.blood_volume -= count2
            if character.blood_volume <= 0:
                character.blood_volume = 0
                # 无尽怒火（借鉴LOL蛮子的大招)：濒死时有(1 + 1 * 技能lv） % 的几率保持一滴血而不死亡
                if character.skills[6].is_learn and random() <= (0.01 + 0.01 * (character.skills[6].rank - 1)):
                    character.blood_volume = 1
                else:
                    return False

            print('----------------------------------------------------')
            print("\033[0;31;40m " + str(damage_factor) + '倍伤害！ ' + str(count1) + ' 人物血量：' + str(
                character.blood_volume) + ' 怪物血量：' + str(monster_blood_volume) + "\033[0m")

            if fight_result is True:
                return True
